# OpenCV+Python+Windows environment setup
# amatea.github.io/295%20OpenCV+Python+Windows%E4%B8%8B%E7%9A%84%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA.html
#
# https://stackoverflow.com/questions/9795601/python-jpeg-to-movie
#
# Opencv encoder
# http://blog.csdn.net/dcrmg/article/details/52215930
#
# Progressbar
# http://blog.ihipop.info/2010/10/1736.html
#
# \\192.168.2.102\lms

import cv2
import os
import sys
import time
#import tqdm
#from progressbar import *



class ProgressBar:
    def __init__(self, count = 0, total = 0, width = 50):
        self.count = count
        self.total = total
        self.width = width
    def move(self):
        self.count += 1
    def log(self, s):
        sys.stdout.write(' ' * (self.width + 9) + '\r')
        sys.stdout.flush()
        print s
        progress = self.width * self.count / self.total
        sys.stdout.write('{0:3}/{1:3}: '.format(self.count, self.total))
        sys.stdout.write('#' * progress + '-' * (self.width - progress) + '\r')
        if progress == self.width:
            sys.stdout.write('\n')
        sys.stdout.flush()

baseFolder = "Z:\\caotang\\"
subStringFolder = "NCD90"
subStringFile = "DSC_"

width = 3216/2
height = 2136/2

startNumber = "0099"
endNumber = "2116"
fileNumber = 0

startFolder = startNumber + subStringFolder
endFolder = endNumber + subStringFolder
    
folders = os.listdir(baseFolder)
folders = sorted(folders)

#fourcc = cv2.cv.CV_FOURCC(*'XVID')
#fourcc = cv2.cv.CV_FOURCC('M','J','P','G')
#fourcc=cv2.cv.CV_FOURCC('M', 'P', '4', '2');
#outputFileName = subFolderFull + ".avi"
outputFileName = startNumber + "-" + endNumber + ".avi"
print "output file: " + outputFileName
#print ""
#out = cv2.VideoWriter(outputFileName, fourcc, 24, (width, height))
out = cv2.VideoWriter(outputFileName, -1, 24, (width, height))
#fourcc = cv2.cv.CV_FOURCC('X','V','I','D')
#out = cv2.VideoWriter(subFolder + ".mp4", fourcc, 24, (1608, 1068))

for subFolder in folders:
    result = subStringFolder in subFolder

    if not result:
        continue

    if subFolder < startFolder or subFolder > endFolder:
        continue
    
    subFolderFull = baseFolder + subFolder
    
    print subFolderFull

    if os.path.isfile(subFolderFull):
        continue

    files = os.listdir(subFolderFull)
    files = sorted(files)

    tmpFileNumber = fileNumber

    #bar = ProgressBar(total = len(files))
    #pbar = ProgressBar().start()
    
    index = 0

    for fileName in files:
        index = index + 1
        result = subStringFile in fileName

        if not result or (0 == (index % 2)):
            continue

        fileNameFull = baseFolder + subFolder + "\\" + fileName
        img = cv2.imread(fileNameFull,-1)
        
        if img is None:
            print "Failed to load " + fileNameFull
            continue

        #orgHeight, orgWidth = img.shape[:2]
        #print "height = %d, width = %d"%(orgHeight, orgWidth)

        try:
            img = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC)
            #cv2.imwrite(fileName, img)
            out.write(img)
            #cv2.imshow('img', img)
            #print "\t" + fileName

            #####
            #bar.move()
            #bar.log(subFolderFull + " Processed " + str(index))

            #####
            #sys.stdout.write('   \r')
            #sys.stdout.flush()
            #sys.stdout.write('{}%'.format(100.0 * float('%.3f'%(index/float(len(files))))))
            #sys.stdout.write('%.1f'%(100.0 * float('%.3f'%(index/float(len(files))))))
            #sys.stdout.flush()

            ####
            #pbar.update(int((i/(len(files)-1))*100))
        except:
            print "Unexpected error:", sys.exc_info()[0]

        if fileNumber > 0:
            tmpFileNumber = tmpFileNumber - 1

            if tmpFileNumber == 0:
                break

    #pbar.finish()
    
    #sys.stdout.write('   \r\n')
    #sys.stdout.flush()

out.release()
cv2.destroyAllWindows()
